package com.deer.wms.base.system.web.bill;

import com.alibaba.fastjson.JSONObject;
import com.deer.wms.base.system.model.bill.BillOutMes;
import com.deer.wms.base.system.model.bill.MaterialInfo;
import com.deer.wms.base.system.model.bill.MaterialInfoCriteria;
import com.deer.wms.base.system.model.zh.CacheLine;
import com.deer.wms.base.system.model.zh.ZhResultMESFinishProduct;
import com.deer.wms.base.system.service.bill.BillOutMesService;
import com.deer.wms.base.system.service.bill.IMaterialInfoService;
import com.deer.wms.base.system.service.task.PickTaskService;
import com.deer.wms.base.system.service.zh.ZhMesService;
import com.deer.wms.base.system.util.JedisServiceUtil;
import com.deer.wms.base.system.web.zhBusiness.out.zhBillOutController;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.result.CommonCode;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import com.deer.wms.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 晶棒托出库管理
 *
 * @author guo
 * @date 2019-05-13
 */
@Controller
@RequestMapping("/out/billOutMaterialInfo")
public class BillOutMaterialController extends BaseController {

    private String prefix = "out/billOutMaterialInfo";
    @Autowired
    private IMaterialInfoService iMaterialInfoService;

    @Autowired
    private BillOutMesService billOutMesService;

    @Autowired
    private ZhMesService zhMesService;

    @Autowired
    private JedisServiceUtil jedisServiceUtil;

    /**
     * 查询整托信息
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialInfoCriteria materialInfoCriteria) {
        startPage();
        List<MaterialInfo> list = iMaterialInfoService.findBillOutList(materialInfoCriteria);
        return getDataTable(list);
    }

    //修改
    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Integer id) {

        return prefix + "/edit";
    }


    /**
     * 晶棒托入库，查看详情时，查询托盘信息
     *
     * @return com.deer.wms.common.core.page.TableDataInfo
     * @author 李帅辰
     * @date 2021/6/9 14:37
     */
    @PostMapping("/billOutMaterialInfoByCarrierId")
    @ResponseBody
    @Transactional
    public TableDataInfo findByMaterialBatchId(String carrierId) {
        startPage();
        List<MaterialInfo> list = iMaterialInfoService.findBillOut(carrierId);
        return getDataTable(list);
    }

    /**
     * 对于过账失败的晶棒手动过账给MES
     * @return void
     * @author 申紫微
     * @date 15:54 2021/7/5
     * @params materialId
     **/
    @PostMapping("/reSendToMES")
    @ResponseBody
    public Result reSendToMES(String materialId) {
        Result result = new Result();
        BillOutMes billOutMesModel = billOutMesService.findBillOutMesByMaterialId(materialId);
        if (billOutMesModel == null) {
            return ResultGenerator.genFailResult(CommonCode.ELECTRIC_ERROR, "此单晶方棒无自动过账数据,无法手动过账给MES", "");
        }
        CacheLine cacheLine = new CacheLine();
        cacheLine.setWmsJson(billOutMesModel.getJson());
        cacheLine.setMaterialId(billOutMesModel.getMaterialId());
        cacheLine.setStockId(billOutMesModel.getStockId());
        cacheLine.setUserId(billOutMesModel.getUserId());
        String mesBackMsg = zhMesService.CancelAccount(cacheLine);
        ZhResultMESFinishProduct resModel = JSONObject.parseObject(mesBackMsg, ZhResultMESFinishProduct.class);
        // 过账成功
        if (resModel.getCode().equals(ZhResultMESFinishProduct.SUCCESS_CODE)) {
            // MES返回过账成功
            billOutMesModel.setResultJosn(JSONObject.toJSONString(resModel));
            billOutMesModel.setMesBackCode(resModel.getCode());
            int j = billOutMesService.updateBillOutMes(billOutMesModel);
            if (j > 0) {
                result = ResultGenerator.genSuccessResult();
                // 手动过账成功，清掉程序自动触发的过账失败次数的累计次数。
                jedisServiceUtil.clean(zhBillOutController.OUT_OK_REQ_MES + billOutMesModel.getMaterialId());
            } else {
                result = ResultGenerator.genFailResult(CommonCode.SERVER_INERNAL_ERROR, "MES过账成功，更新WMS过账成功状态失败", materialId);
                logger.error("MES过账成功，更新WMS过账成功状态失败,任务号:{}", materialId);
            }
        }
        return result;
    }

    @RequiresPermissions("out:billOutMaterialInfo:view")
    @GetMapping("/page")
    public String MaterialInfo() {
        return prefix + "/billOutMaterialInfo";
    }


    @GetMapping("/detail")
    public String billInDetail() {
        return prefix + "/detail";
    }


    /***
     *导出
     * @author 李帅辰
     * @date 2020/9/24 11:06
     * @return com.deer.wms.common.core.domain.AjaxResult
     */
    @RequiresPermissions("out:billOutMaterialInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialInfoCriteria criteria) {
        List<MaterialInfo> list = iMaterialInfoService.findList(criteria);
        ExcelUtil<MaterialInfo> util = new ExcelUtil<MaterialInfo>(MaterialInfo.class);
        return util.exportExcel(list, "晶棒托出库报表");
    }

    /**
     * 导出列表
     */
    @RequiresPermissions("out:billOutMaterialInfo:partExcel")
    @PostMapping("/partExcel")
    @ResponseBody
    public AjaxResult partExcel(@RequestParam String ids) {
        List<MaterialInfo> byIds = new ArrayList();
        String[] split = ids.split(",");
        for (String s : split
        ) {
            MaterialInfoCriteria b = new MaterialInfoCriteria();
            b.setCarrierId(s);
            List<MaterialInfo> list = iMaterialInfoService.findBillOutList(b);
            for (MaterialInfo bs : list
            ) {
                byIds.add(bs);
            }


        }
        // List<BillInSapDto> billInSapList = billInSapService.findBillInSapDtoList(billInSapCriteria);
        ExcelUtil<MaterialInfo> util = new ExcelUtil<MaterialInfo>(MaterialInfo.class);
        return util.exportExcel(byIds, "晶棒托出库报表");
    }


}
